package zetcode.sprites;

import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import javax.swing.ImageIcon;

public class Craft {
	private String craft = "/pics/craft.png";
	
	private int dx, dy;
	private int x, y;
	private int width, height;
	private boolean visible;
	private Image image;
	private ArrayList<Missile> missiles;
	
	public Craft(){
		ImageIcon ii = new ImageIcon(this.getClass().getResource(craft));
		image = ii.getImage();
		width = image.getWidth(null);
		height = image.getHeight(null);
		missiles = new ArrayList<Missile>();
		visible = true;
		x = 40;
		y = 60;
	}
	
	public void move(){
		x += dx;
		y += dy;
		
		if(x < 1){x = 1;}
		if(y < 1){y = 1;}
	}
	
	public void fire(){
		missiles.add(new Missile(x + width, y + height/2));
	}
	
	public int getX(){return x;}
	public int getY(){return y;}
	public Image getImage(){return image;}
	public ArrayList<Missile> getMissiles(){return missiles;}
	public boolean isVisible(){return visible;}
	public Rectangle getBounds(){return new Rectangle(x, y, width, height);}
	
	public void setVisible(boolean visible){this.visible = visible;}
	
	public void keyPressed(KeyEvent e){
		int key = e.getKeyCode();
		
		if(key == KeyEvent.VK_LEFT){dx = -1;}
		if(key == KeyEvent.VK_RIGHT){dx = 1;}
		if(key == KeyEvent.VK_UP){dy = -1;}
		if(key == KeyEvent.VK_DOWN){dy = 1;}
		if(key == KeyEvent.VK_SPACE){fire();}
	}
	
	public void keyReleased(KeyEvent e){
		int key = e.getKeyCode();
		
		if(key == KeyEvent.VK_LEFT){dx = 0;}
		if(key == KeyEvent.VK_RIGHT){dx = 0;}
		if(key == KeyEvent.VK_UP){dy = 0;}
		if(key == KeyEvent.VK_DOWN){dy = 0;}
	}
}
